<template>
  <div class="mt-1">
    <van-form @submit="handleSubmit">
      <h4 style="margin: 0 0 0.4rem 1rem; color: gray">
        当前产区:{{ this.$store.getters.productionLine }}
      </h4>
      <van-field
        readonly
        clickable
        name="picker"
        :value="selectedStatus.F_ItemName"
        label="状态调整"
        placeholder="点击选择"
        @click="statusPicker = true"
      />
      <van-popup v-model="statusPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="columns"
          value-key="F_ItemName"
          @confirm="statusAdjustConfirm"
          @cancel="statusPicker = false"
        />
      </van-popup>
      <van-field
        v-model="code"
        clearable
        label="拍码"
        ref="pmfocus"
        @focus="stopKeyborad"
        placeholder="请输入拍码"
      />
      <ag-grid-vue
        style="width: 100%; height: 55vh"
        class="ag-theme-alpine mt-1"
        :columnDefs="columnDefs"
        :defaultColDef="defaultColDef"
        :gridOptions="gridOptions"
        :singleClickEdit="true"
        rowSelection="multiple"
        :rowData="rowData"
      >
      </ag-grid-vue>
      <van-button block type="info" class="mt-1">提交</van-button>
    </van-form>
  </div>
</template>

<script>
import { AgGridVue } from "ag-grid-vue";
import { CODE_LENGTH } from "../../config";
import { AgGrid } from "../../mixins/AgGrid";
import { pat, statusAdjusts, statusAdjustSubmit } from "../../api/material";
import { parseQRCode, toast } from "../../utils/common";
import { Picker } from "vant";
import { isEmpty } from "ramda";

export default {
  name: "StateAdjustment",
  index: "",
  components: { AgGridVue, [Picker.name]: Picker },
  mixins: [AgGrid],
  data() {
    return {
      code: "",
      submitSure: true,
      columnDefs: [
        { headerName: "物料号", field: "ProductId", width: 90 },
        { headerName: "物料名称", field: "Producer", width: 90 },
        { headerName: "生产日期", field: "ProductionDate", width: 120 },
        {
          headerName: "数量",
          field: "StockQuantity",
          width: 70,
          editable: true,
        },
      ],
      rowData: [],
      statusPicker: false,
      columns: [],
      rowPallenNoNum: [],
      selectedStatus: {},
    };
  },
  watch: {
    code(val) {
      if (!isEmpty(this.selectedStatus)) {
        if (val.length > CODE_LENGTH) {
          let res = parseQRCode(val);
          if (res) {
            this.findPat(res.palletNo);
            this.$refs.pmfocus.focus();
          } else {
            toast("无法识别");
          }
        }
      } else {
        toast("请先选择状态调整");
      }
      this.code = "";
    },
  },
  methods: {
    // 消除获取焦点时自动弹出键盘
    stopKeyborad() {
      this.$refs.pmfocus.$refs.input.setAttribute("readonly", "readonly");
      setTimeout(() => {
        this.$refs.pmfocus.$refs.input.removeAttribute("readonly");
      }, 200);
    },
    async findPat(palletNo) {
      if (!this.rowData.find((it) => it.UniqueId === palletNo)) {
        let Inventory = this.columns[this.index].F_ItemCode;
        let res = await pat(
          this.$store.getters.productionLine,
          palletNo,
          Inventory,
          this.$store.getters.WarehouseId
        );
        console.log(res);
        if (res && res.Status == 200 && res.Data) {
          if (res.Data.Inventorystatus === this.selectedStatus.F_ItemCode) {
            this.rowData.push(res.Data);
          } else {
            toast("所选状态调整的状态与库存物料状态不同");
          }
        } else {
          toast(res.Msg);
        }
      } else {
        toast("此拍已被录入");
      }
    },
    //提交
    async handleSubmit() {
      if (this.rowData.length > 0) {
        let user = this.$store.getters.user;
        let productionline = this.$store.getters.productionLine;
        let F_ItemCode = this.columns[this.index].F_ItemCode;
        let F_Field1 = this.columns[this.index].F_Field1;
        let F_Id = this.columns[this.index].F_Id;
        let F_ItemName = this.columns[this.index].F_ItemName;
        this.getPalletNoNum();
        let res = await statusAdjustSubmit(
          user,
          productionline,
          F_ItemCode,
          F_Field1,
          F_Id,
          F_ItemName,
          this.rowPallenNoNum
        );
        if (res.Status == 200) {
          toast("提交已完成");
          this.submitSure = true;
          this.rowPallenNoNum.length = 0;
          this.rowData = [];
        } else {
          toast(res.Msg);
          this.submitSure = false;
        }
      } else {
        toast("数据为空");
      }
      this.$refs.pmfocus.focus();
    },
    //将表单里的数据归类
    getPalletNoNum() {
      if (this.submitSure) {
        let _that = this;
        this.gridApi.forEachNode((node) => {
          _that.rowPallenNoNum.push(node.data.UniqueId);
        });
      }
    },
    rowSelected(id) {
      this.gridApi.forEachNode((node) => {
        node.setSelected(node.data.make === id);
      });
    },
    statusAdjustConfirm(val, index) {
      this.selectedStatus = val;
      this.statusPicker = false;
      this.index = index;
      this.$refs.pmfocus.focus();
    },
    //得到状态的数据
    async getStatusAdjusts() {
      let res = await statusAdjusts();
      if (res && res.status === 200 && res.data.length) {
        this.columns = res.data;
        console.log(this.columns);
      } else {
        toast("无法获取状态调整选项，请联系管理员");
      }
    },
  },
  mounted() {
    this.getStatusAdjusts();
    this.$refs.pmfocus.focus();
  },
};
</script>
